﻿using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System.Text;
 
public class eduProgramClient : MonoBehaviour 
{
    private static eduProgramClient _instance;

    public static eduProgramClient Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance =new eduProgramClient();
                //_instance = new GameObject("SimpleTcpClient").AddComponent<SimpleTcpClient>();
                
            }

            return _instance;
        }
    }
    private static Socket serv_Socket;

    public string iPAdress = "220.67.124.155";
    public const int kPort = 6000;
        
    private byte[] SendData;                        // Data encoding to send. ( to Change bytes)
    private byte[] RecvData = new byte[2000];    // Receive data by this array to save.
    private string RecvString;                     // Receive bytes to Change string. 
    private int result;

    public GameObject inputBox;
    public GameObject inputBox1;
    UIInput uiInput;
    UIInput uiInput1;
    private string idField;
    private string passField;
    public static string id;
    bool wrong = false;
    
    
    void Awake ()
    {
        serv_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        serv_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000);
        serv_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);

        try
        {
            IPAddress ipAddr = System.Net.IPAddress.Parse(iPAdress); 
            IPEndPoint ipEndPoint = new System.Net.IPEndPoint(ipAddr, kPort);
            serv_Socket.Connect(ipEndPoint);
        }
        catch (SocketException SCE)
        {
            Debug.Log("Socket connect error! : " + SCE.ToString() ); 
            return;
        }       
    }
    
    public void getId()
    {
        uiInput = inputBox.GetComponent<UIInput>();
        idField = uiInput.label.text;
        string[] idField1 = idField.Split('|');
        idField = idField1[0];
        id = idField;
    }

    public void getPass()
    {
        uiInput1 = inputBox1.GetComponent<UIInput>();
        passField = uiInput1.label.text;
        string[] passField1 = passField.Split('|');
        passField = passField1[0];
        
    }

    public void connect()
    {
        string loginCheck = "USER " + idField + " " + passField;
        result = msgHandle(loginCheck);
        if (result == 230)
        {
            Application.LoadLevel("mainMenu");
        }
        else
        {
            wrong = true;
        }
        
    }
   
    public void OnApplicationQuit ()
    {
        serv_Socket.Close();
        serv_Socket = null;
        _instance = null;
    }

    public int msgHandle(string input)
    {
        string[] handling = null;
        int temp;
        try
        {
            SendData = Encoding.Default.GetBytes(input.ToString());
            serv_Socket.Send(SendData, SendData.Length, 0);

            serv_Socket.Receive(RecvData);
            RecvString = Encoding.Default.GetString(RecvData);
            Debug.Log("Receive Data : " + RecvString);
        }
        catch (SocketException err)
        {
            Debug.Log("Socket send or receive error! : " + err.ToString());
        }
        handling = RecvString.Split(' ');
        temp = handle(handling[0]);
        return temp;
    }

    public int handle(string input)
    {
        int num = int.Parse(input);
        switch (num)
        {
            case 204:
                // 구매한 펫 보여주기? 도감으로 이동하기?
                break;
            case 301:
                // 유저데이터 보여주기
                break;
            default:
                break;
        }
        return num;
    }

    void OnGUI()
    {
        GUIStyle style = new GUIStyle();
        style.fontSize = 20;
        style.normal.textColor = Color.black;
        var stringToEdit = "ID or Pass is wrong!";
        if (wrong == true)
        {
            GUI.Label(new Rect(400, 200, 130, 50), stringToEdit, style);
        }
        
    }
} 

